extends Control

#节点加载---start
@onready var start = $Start
@onready var back = $Back
#节点加载---end

var ready_scene
var round_scene
var play_scene
var ready_scene_packed_scene : PackedScene
var round_scene_packed_scene : PackedScene
var play_scene_packed_scene : PackedScene

func _ready() -> void:
	# 预加载场景文件
	ready_scene_packed_scene = preload("res://GameScenes/ReadyScene/Ready.tscn")
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)
	
	round_scene_packed_scene = preload("res://GameScenes/SelectRoundScene/SelectRound.tscn")
	play_scene_packed_scene = preload("res://GameScenes/PlayScene/Play.tscn")
	#绑定按钮事件
	start.connect("pressed", Callable(self, "_onStart"))
	back.connect("pressed", Callable(self, "_onBack"))

#start按钮事件
func _onStart():
	start.visible = false #隐藏start按钮
	ready_scene.queue_free() #销毁ready页面
	round_scene = round_scene_packed_scene.instantiate() #展示回合页面
	add_child(round_scene)
	round_scene.mouse_filter = Control.MOUSE_FILTER_IGNORE
	round_scene.visible = true
	# 连接回合场景的信号到main场景
	round_scene.connect("start_game", Callable(self, "_on_start_game"))

func _on_start_game():
	round_scene.queue_free() #销毁round页面
	play_scene = play_scene_packed_scene.instantiate() #展示游玩页面
	add_child(play_scene)
	play_scene.visible = true
	play_scene.connect("success_game",Callable(self,"_on_success_game"))
	play_scene.connect("lose_game",Callable(self,"_on_lose_game"))
	
func _on_success_game():
	start.visible = true #展示start按钮
	play_scene.queue_free()
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)

func _on_lose_game():
	start.visible = true #展示start按钮
	play_scene.queue_free()
	#将玩家detail初始化
	PlayerDetail.currentFunctionCardDeck = []
	PlayerDetail.currentCarryFunctionCardDeck = []
	PlayerDetail.currentCarryFunctionProps = []
	PlayerDetail.playerMoney = 0
	PlayerDetail.playerDeck = Deck.new().getDeck()
	RoundDetail.currentBigRound = 1
	RoundDetail.currentSmallRound = 1
	
	#商店初始化
	ShopDetail.functionCardDeck = ShopDetail.copyDeck
	#道具初始化
	PropDetail.functionPropDeck = PropDetail.copyDeck
	
	ready_scene = ready_scene_packed_scene.instantiate()
	add_child(ready_scene)
